package logic

import (
	"context"
	"errors"
	"video/models/mysql"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectionDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectionDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectionDelLogic {
	return &CollectionDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 取消收藏
func (l *CollectionDelLogic) CollectionDel(in *videorpc.CollectionDelRequest) (*videorpc.CollectionDelResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("登录之后,才能取消收藏")
	}
	var Collection mysql.Collection
	err = Collection.FindComment(mysql.DB, in.UserId, in.VideoId)
	if err != nil {
		return nil, err
	}
	if Collection.ID == 0 {
		return nil, errors.New("已取消收藏")
	}

	err = Collection.CommentDel(mysql.DB, in.UserId, in.VideoId)
	if err != nil {
		return nil, errors.New("取消收藏失败")
	}
	return &videorpc.CollectionDelResponse{}, nil
}
